<template>
  <el-container style="height: 100vh; border-radius: 20px; overflow: hidden;">
    <el-header style="background-color: #fff; padding: 15px; display: flex; justify-content: space-between; align-items: center; border-radius: 20px 20px 0 0; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);">
      <h2 style="font-size: 24px; font-weight: 600; color: #333; margin: 0;">管理员控制台</h2>
      <div style="display: flex; align-items: center;">
        <span style="font-size: 16px; color: #333;">{{ userName }}</span>
        <el-button @click="logout" size="large" type="text" style="margin-left: 15px; font-weight: bold; color: #ff4d4f; padding: 6px 10px; font-size: 20px; border-radius: 20px;">
          <i class="el-icon-link" style="margin-right: 10px; font-size: 24px;"></i>退出
        </el-button>
      </div>
    </el-header>

    <el-container style="flex: 1; display: flex; overflow: hidden;">
      <el-aside width="250px" style="background-color: rgb(238, 241, 246); display: flex; flex-direction: column; justify-content: flex-start; height: calc(100vh - 60px); overflow: hidden;">
        <el-menu :default-active="activeMenu" @select="handleMenuSelect">
          <el-menu-item index="1-1">
            <i class="el-icon-chat-dot-round"></i>
            <span>查看反馈</span>
          </el-menu-item>
          <el-menu-item index="1-0">
            <i class="el-icon-user"></i>
            <span>账户管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main style="flex-grow: 1; display: flex; flex-direction: column; background-color: rgba(255, 255, 255, 0.9); box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1); overflow: hidden;">
        <!-- 动态加载的内容 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  name: 'AdminView',
  computed: {
    ...mapState({
      userName: state => state.userInfo.username,
      userAvatar: state => state.userInfo.avatar,
      isAdmin: state => state.userInfo.isAdmin,
    }),
    activeMenu() {
      if (this.$route.path === '/admin/feedbackHistory') {
        return '1-1';
      } else if (this.$route.path === '/admin/manageAccounts') {
        return '1-0';
      }
      return '';
    }
  },
  mounted() {
    // 页面加载时，检查JWT token
    const token = sessionStorage.getItem('jwt_token');
    if (!token) {
      // 没有 token，跳转到登录页面
      this.$router.push('/login');
    } else {
      // 如果 token 存在，使用 token 获取用户信息
      this.fetchUserInfo();
    }
  },
  methods: {
    ...mapActions(['fetchUserInfo']),

    logout() {
      sessionStorage.removeItem('jwt_token');
      this.$router.push('/login');
    },

    handleMenuSelect(index) {
      let targetPath = '';
      if (index === '1-1') {
        targetPath = '/admin/feedbackHistory';
      } else if (index === '1-0') {
        targetPath = '/admin/manageAccounts';
      }

      // 如果当前路由与目标路径不同，才进行导航
      if (this.$route.path !== targetPath) {
        this.$router.push(targetPath);
      }
    }
  }
};
</script>

<style scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden; /* 禁止滚动 */
}

.el-container {
  height: 100vh; /* 使容器高度与视口高度相同 */
  overflow: hidden; /* 禁止滚动 */
}

.el-header {
  background-color: #fff;
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  flex-shrink: 0; /* 防止头部内容被挤压 */
}

.el-aside {
  background-color: rgb(238, 241, 246);
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  height: calc(100vh - 60px); /* 让侧边栏的高度为视口高度减去头部的高度 */
  overflow: hidden; /* 禁止内容超出 */
}

.el-main {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  background-color: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  overflow: hidden; /* 禁止内容超出 */
}

.el-menu {
  width: 100%;
}

.el-button {
  margin-left: 10px;
  border-radius: 30px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.el-button i {
  margin-right: 5px;
}

.el-button:hover {
  transform: scale(1.05);
}
</style>
